package database

import (
	"sync"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"

	"test.com/app/headlers"
	"test.com/config"
)

var (
	eloquent *gorm.DB
	once     sync.Once
)

// GetEloquent 返回数据库链接
func GetEloquent() *gorm.DB {
	var err error
	// 执行一次
	once.Do(func() {
		mysqlConfig := config.GetMysqlConfig()
		eloquent, err = gorm.Open("mysql", mysqlConfig.User+
			":"+mysqlConfig.Password+"@/"+mysqlConfig.Database+
			"?charset=utf8&parseTime=True&loc=Local")
		headlers.CheckErr(err)
	})
	return eloquent
}
